package gui;


import shop.DateTransform;
import shop.Main;
import shop.Mall;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;



/**
 * @author Wray
 */
public class MallGui {
    JFrame mallGui = new JFrame("商城");

    public MallGui() {
        initMallGui();
    }


    private void initMallGui() {

        mallGui.setLayout(null); // 清除布局函数
        mallGui.setResizable(false); // 设置窗体大小不可变

        mallGui.setLayout(new BorderLayout()); // 新建BorderLayout布局



        // 中间
        JPanel panelCenter = new JPanel(new FlowLayout());

        DefaultTableModel model = new DefaultTableModel() { // 表格数据不可改
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        Vector<Vector<Object>> date = new Vector<Vector<Object>>();
        Vector<String> names = new Vector<String>();
        names.add("商品名");
        names.add("单价(元)");
        names.add("库存");
        DateTransform.changeList(Mall.getMallHashMap(),date);
        model.setDataVector(date, names);

        JTable commodityTable = new JTable(model);// 创建一个表格，指定 所有行数据 和 表头
        SettingWindow.setTable(commodityTable); // 设置表格

        JScrollPane commodityScrollPane = new JScrollPane(commodityTable);

        panelCenter.add(commodityScrollPane);
        panelCenter.setBorder(new EmptyBorder(10, 10, 10, 10));
        mallGui.add(panelCenter, BorderLayout.CENTER);


        // 标题
        JPanel panelTitle = new JPanel(new GridLayout(2, 0));
        JLabel jl1 = new JLabel("商品列表");
        jl1.setFont(new Font("微软雅黑", Font.BOLD, 25)); // 设置字体、样式、大小
        JLabel jl2 = new JLabel("共 " + commodityTable.getRowCount() + " 件宝贝");
        jl2.setFont(new Font("微软雅黑", Font.PLAIN, 19)); // 设置字体、样式、大小
        panelTitle.add(jl1);
        panelTitle.add(jl2);
        panelTitle.setBorder(new EmptyBorder(10, 10, 10, 10));
        mallGui.add(panelTitle, BorderLayout.NORTH);

        //按钮
        JPanel buttonPanel=new JPanel();
        JButton addShoppingCartButton=new JButton("加入购物车");
        addShoppingCartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String inputValue = JOptionPane.showInputDialog("购买数量");
                int count = Integer.parseInt(inputValue);

                int row = commodityTable.getSelectedRow();
                Main.person.shoppingCart.addCommodity((String) model.getValueAt(row,0),count);
               //ShoppingCartFrame.setShoppingCartCenterPanel(1);
            }
        });
        buttonPanel.add(addShoppingCartButton);
        mallGui.add(buttonPanel,BorderLayout.SOUTH);



        // Display the window.
        mallGui.setSize(450, 750);
        mallGui.setVisible(false);
        // 设置窗口居中显示
        SettingWindow.setFrameNear(mallGui);
        //mallGui.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }}

//    public JTable getProductList() {
//        return productListTable;
//    }
//
//    public void setProductList(JTable productList) {
//        productListTable = productList;
//    }
//
//    public JFrame getShoppingCarGui() {
//        return productListGui;
//    }
//
//    public void setShoppingCarGui(JFrame shoppingCarGui) {
//        productListGui = shoppingCarGui;
//    }


